layui.define(['table', 'form', 'admin', 'upload', 'element', 'laypage'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , element = layui.element
        , upload = layui.upload
        , admin = layui.admin;

    var room_id = $('#roomId').val();
    var nowCurr = 0;
    var nowCount = 0;
    // 获取聊天日志列表
    var owndefine = {
        getList: function (data) {
            return {
                elem: '#LAY-ads-applaunch-manage'
                , url: '/admin/chat/getlist'
                , where: {room_id: room_id}
                , cols: [[
                    {field: 'user_id', title: '用户ID', width: 110, fixed: 'left'}
                    , {field: 'nick_name', title: '用户昵称', width: 120}
                    , {
                        field: '', title: '用户等级', width: 100, templet: function (d) {
                            return 'LV' + d.user_level;
                        }
                    }
                    , {
                        field: '', title: '用户贵族', width: 100, templet: function (d) {
                            return d.nobel_str ? d.nobel_str : '无';
                        }
                    }
                    , {field: 'mobile', title: '手机号', width: 120}
                    , {
                        field: '', title: '用户状态', width: 100, templet: function (d) {
                            return d.bannerd_status == 2 ? '<font color="red">被禁言</font>' : '正常';
                        }
                    }
                    , {field: 'room_id', title: '房间id', width: 100}
                    , {field: 'create_time', title: '发言时间', width: 180}
                    , {field: 'content', title: '发言内容'}
                    , {title: '操作', width: 250, align: 'center', fixed: 'right', toolbar: '#table-chat_historylist'}
                ]]
                , page: true
                , limit: 20
                , height: 'full-140'
                , text: '对不起，加载出现异常！'
                , even: true

                , done: function (res, curr, count) {
                    //隐藏下面的分页
                    // $(".layui-table-page").css("display","none");
                    //拿到当前每页的分页总数
                    let limitCount = insTb.config.limit;
                    //总数除以每页要显示的数据可以拿到要分多少次页
                    let result = Math.ceil(count / limitCount);
                    nowCurr = curr;
                    nowCount = count;
                    //如果大于最大的分页，则直接把分页重新设置为1，则从第一页重新开始刷新
                    if (result < nowCurr) {
                        nowCurr = 1;
                    }
                }
            }
        }
    }


    var insTb = table.render(owndefine.getList({}));
    var time = setInterval(function () {
        if (nowCurr == 1 && room_id > 0) {
            insTb.reload({page: {curr: nowCurr}});
        }
    }, 5000);


    var elem_config = ["LAY-ads-applaunch-manage", 'LAY-ads-apphome-manage', 'LAY-ads-apphomepop-manage', 'LAY-ads-svideo-manage', 'LAY-ads-appmoments-manage'];
    var current_index = 0;
    element.on('tab(tabBrief)', function (data) {
        current_index = data.index;
        if (current_index == 1) {
            table.render({
                elem: '#LAY-ads-apphome-manage'
                , url: '/admin/chat/getlist?class=2'
                , where: {type: 1}
                , cols: [[
                    {field: 'account', width: 200, title: '账号', sort: true}
                    , {
                        field: 'free_second', width: 200, title: '剩余禁言时长(秒)', templet: function (d) {
                            return d.free_second ?? '';
                        }
                    }
                    , {field: 'created_at', title: '时间', width: 100}
                    // , {field: '', maxWidth: 100, title: '操作人', templet: function (d) {
                    //         return d.admin_name??'';
                    //     }}
                    // , {field: '', minWidth: 180, title: '禁言备注', templet: function (d) {
                    //         return d.admin_note??'';
                    //     }}
                ]]
                , page: true
                , limit: 20
                , height: 'full-140'
                , text: '对不起，加载出现异常！'
                , even: true
            });
        } else {
            table.render(owndefine.getList(data));
        }

    });


    //监听工具条
    table.on("tool(LAY-ads-applaunch-manage)", function (obj) {
        if (obj.event == "bannerd") {
            layer.open({
                type: 2,
                title: "确定禁言该用户吗?",
                content: "/admin/user/setBanTime?type=2&id=" + obj.data.user_id + '&room_id=' + obj.data.room_id + '&chat_id=' + obj.data.id,
                maxmin: true,
                area: ["700px", "530px"],
            });
        } else if (obj.event == "unbannerd") {
            layer.confirm("确定解除禁言？", function (index) {
                layer.load();
                admin.req({
                    url: "/admin/user/unBanned",
                    data: {id: obj.data.bannedid, room_id: obj.data.room_id},
                    success(res) {
                        if (res.code == 0) {
                            table.reload("LAY-ads-applaunch-manage");
                            layer.msg(res.msg, {icon: 1});
                        }
                        layer.closeAll("loading");
                    },
                    complete: function () {
                        layer.closeAll("loading");
                    },
                });
            });
        } else if (obj.event == "shieldspeak") {
            layer.confirm("确定要屏蔽发言吗？", function (index) {
                layer.load();
                admin.req({
                    url: "/admin/chat/shield_speak",
                    data: {id: obj.data.id, user_id: obj.data.user_id, room_id: obj.data.room_id},
                    success(res) {
                        if (res.code == 0) {
                            layer.msg(res.msg, {
                                icon: 1,
                                time: 3000
                            }, function () {
                                table.reload("LAY-ads-applaunch-manage");
                            });
                        }
                        layer.closeAll("loading");
                    },
                    complete: function () {
                        layer.closeAll("loading");
                    },
                });
            });
        }
    });


    exports('chat', {})
});